package com.k12.admim.controller.operator;

import cn.dev33.satoken.stp.StpUtil;
import com.k12.admim.model.vo.sys.SysDeptVo;
import com.k12.admim.service.OperatorDeptService;
import com.k12.admim.utils.RequestContextUtil;
import com.k12.common.model.resp.R;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class OperatorDeptController {

    @Resource
    private OperatorDeptService operatorDeptService;

    @GetMapping("system/dept/list")
    public R listDept(SysDeptVo dept) {
        return R.success(operatorDeptService.selectDeptList(dept));
    }

    @PostMapping("system/dept")
    public R add(@RequestBody SysDeptVo dept) {
        String loginId = StpUtil.getLoginId().toString();
        String tenantId = RequestContextUtil.getTenantId();
        if(StringUtils.isBlank(tenantId)){
            return R.failed("非法登录");
        }
        dept.setTenantId(Long.valueOf(tenantId));
        if (!operatorDeptService.checkDeptNameUnique(dept)) {
            return R.failed("新增部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }
        dept.setCreateBy(loginId);
        return operatorDeptService.insertDept(dept);
    }

    @GetMapping("system/dept/{deptId}")
    public R getInfo(@PathVariable Long deptId) {
        String tenantId = RequestContextUtil.getTenantId();
        if(StringUtils.isBlank(tenantId)){
            return R.failed("非法登录");
        }
        boolean b = operatorDeptService.checkDeptDataScope(deptId, tenantId);
        if (b){
            return operatorDeptService.selectDeptById(deptId,tenantId);
        }else{
            return R.failed("没有权限");
        }
    }

    @GetMapping("system/dept/list/exclude/{deptId}")
    public R exclude(@PathVariable Long deptId) {
        String tenantId = RequestContextUtil.getTenantId();
        if(StringUtils.isBlank(tenantId)){
            return R.failed("非法登录");
        }
        SysDeptVo dept = new SysDeptVo();
        dept.setTenantId(Long.valueOf(tenantId));
        List<SysDeptVo> sysDeptVos = operatorDeptService.selectDeptList(dept);
        sysDeptVos.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
        return R.success(sysDeptVos);
    }


    @PutMapping("system/dept")
    public R update(@Validated @RequestBody SysDeptVo dept) {
        Long deptId = dept.getDeptId();
        String tenantId = RequestContextUtil.getTenantId();
        if(StringUtils.isBlank(tenantId)){
            return R.failed("非法登录");
        }
        boolean b = operatorDeptService.checkDeptDataScope(deptId, tenantId);
        if (!operatorDeptService.checkDeptNameUnique(dept)) {
            return R.failed("修改部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        } else if (dept.getParentId().equals(deptId)) {
            return R.failed("修改部门'" + dept.getDeptName() + "'失败，上级部门不能是自己");
        } else if (StringUtils.equals("1", dept.getStatus()) && operatorDeptService.selectNormalChildrenDeptById(deptId,tenantId) > 0) {
            return R.failed("该部门包含未停用的子部门！");
        }
        if (b){
            operatorDeptService.updateDept(dept);
            return R.success();
        }else{
            return R.failed("没有权限");
        }

    }

    /**
     * 删除部门
     */
    @DeleteMapping("system/dept/{deptId}")
    public R remove(@PathVariable Long deptId) {
        String tenantId = RequestContextUtil.getTenantId();
        if(StringUtils.isBlank(tenantId)){
            return R.failed("非法登录");
        }

        if (operatorDeptService.hasChildByDeptId(deptId,tenantId)) {
            return R.failed("存在下级部门,不允许删除");
        }

        if (operatorDeptService.checkDeptExistUser(deptId,tenantId)) {
            return R.failed("部门存在用户,不允许删除");
        }
        boolean b = operatorDeptService.checkDeptDataScope(deptId, tenantId);
        if (b){
            operatorDeptService.deleteDeptById(deptId,tenantId);
            return R.success();
        }else{
            return R.failed("没有权限");
        }
    }


}
